#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
数据库验证脚本
"""

import sys
import logging
from pathlib import Path

# 添加项目根目录到Python路径
project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root))

from src.database.connection import db_manager
from src.database.models import City, BusinessType, RealEstateLead, CrawlTask, DataQualityLog


def main():
    """主函数"""
    # 配置日志
    logging.basicConfig(
        level=logging.INFO,
        format='%(asctime)s [%(levelname)s] %(name)s: %(message)s'
    )
    
    logger = logging.getLogger(__name__)
    
    try:
        logger.info("开始验证数据库...")
        
        with db_manager.get_session() as session:
            # 验证城市数据
            cities = session.query(City).all()
            logger.info(f"城市数量: {len(cities)}")
            for city in cities:
                logger.info(f"  - {city.city_name} ({city.city_code})")
            
            # 验证业态数据
            business_types = session.query(BusinessType).all()
            logger.info(f"业态数量: {len(business_types)}")
            for bt in business_types:
                logger.info(f"  - {bt.type_name} ({bt.type_code})")
            
            # 验证表结构
            logger.info("验证表结构...")
            tables = [
                ('cities', City),
                ('business_types', BusinessType),
                ('real_estate_leads', RealEstateLead),
                ('crawl_tasks', CrawlTask),
                ('data_quality_logs', DataQualityLog)
            ]
            
            for table_name, model_class in tables:
                count = session.query(model_class).count()
                logger.info(f"表 {table_name}: {count} 条记录")
        
        logger.info("数据库验证完成！")
        
    except Exception as e:
        logger.error(f"数据库验证失败: {e}")
        sys.exit(1)


if __name__ == "__main__":
    main()
